package com.tjise.eduservice.controller;


import com.tjise.commonutils.R;
import com.tjise.eduservice.entity.EduChapter;
import com.tjise.eduservice.entity.chapter.ChapterVo;
import com.tjise.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author TJISE
 * @since 2021-06-23
 */
@Api(description = "章节管理")
@RestController
@RequestMapping("/eduservice/chapter")

public class EduChapterController {
    // 注入Chapterservice
    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 返回课程大纲的列表,根据课程id进行查询
     */
    @ApiOperation("根据课程id获取课程的大纲列表")
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable("courseId") String courseId) {
        List<ChapterVo> list = eduChapterService.getChapterVoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);
    }

    /**
     * 添加章节
     */
    @ApiOperation(value = "章节的 添加")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    /**
     * 根据章节id查询
     */
    @ApiOperation("根据api查询章节")
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable("chapterId") String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("chapter", eduChapter);

    }

    /**
     * 修改章节
     */
    @ApiOperation("修改章节")
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    /**
     * 删除的方法
     * 如果章节有小节
     * 如果章节没有小节
     * 1、 删除章节的时候 章节中的所有小节全部删除
     * 2、如果删除的章节下边有小节，不让它进行删除
     */
    @ApiOperation(value = "删除章节")
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable("chapterId") String chapterId) {
        boolean flag = eduChapterService.deleteChapter(chapterId);
        if (flag) {
            return R.ok();
        } else {

            return R.error();
        }
    }


}

